package com.hanson.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hanson.common.bean.ResultMessage;
import com.hanson.common.util.PageInfo;
import com.hanson.entity.GoodsTypeBean;
import com.hanson.entity.IndexNavBean;
import com.hanson.service.GoodsTypeService;
import com.hanson.service.IndexNavService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("/admin/indexNav")
public class IndexNavsController {
    @Autowired
    private IndexNavService indexNavService;
    @Autowired
    private GoodsTypeService goodsTypeService;

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String login(ModelMap modelMap) {
        return "admin/indexNav/index";
    }

    @RequestMapping(value = "/modify", method = RequestMethod.GET)
    public String modify(ModelMap modelMap, String id) {
        Map<String, Integer> dataMap = new HashMap<>();
        if (StringUtils.isNotEmpty(id)) {
            IndexNavBean indexNavBean = indexNavService.detail(Integer.parseInt(id));
            modelMap.put("data", indexNavBean);
            if (StringUtils.isNotEmpty(indexNavBean.getTypeId())) {
                for (String typeId : indexNavBean.getTypeId().split(",")) {
                    dataMap.put(typeId, 1);
                }
            }
        }
        List<GoodsTypeBean> goodsTypeBeanList = goodsTypeService.selecRootType(0);
        List<JSONObject> options = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(goodsTypeBeanList)) {
            goodsTypeBeanList.stream().forEach(c -> {
                JSONObject rootObject = new JSONObject();
                rootObject.put("id", c.getId());
                rootObject.put("title", c.getCateName());
                rootObject.put("expand", false);
                if(dataMap.containsKey(String.valueOf(c.getId()))){
                    rootObject.put("checked", true);
                }
                List<GoodsTypeBean> childList = goodsTypeService.selecRootType(c.getId());
                List<JSONObject> childs = new ArrayList<>();
                if (CollectionUtils.isNotEmpty(childList)) {
                    childList.stream().forEach(a -> {
                        JSONObject childObject = new JSONObject();
                        childObject.put("id", a.getId());
                        childObject.put("title", a.getCateName());
                        if(dataMap.containsKey(String.valueOf(a.getId()))){
                            childObject.put("checked", true);
                        }
                        childs.add(childObject);
                    });
                }
                rootObject.put("children", childs);
                options.add(rootObject);
            });
            modelMap.put("typeList", JSON.toJSONString(options));
        }
        return "admin/indexNav/modify";
    }

    @RequestMapping(value = "listData", method = RequestMethod.GET)
    @ResponseBody
    public PageInfo<IndexNavBean> listData(int limit, int page, IndexNavBean indexNavBean) {
        Page<IndexNavBean> pageInfo = new Page<>(page, limit);
        return new PageInfo(indexNavService.listIndexNavs(pageInfo, indexNavBean));
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage add(@RequestBody IndexNavBean indexNavBean) {
        ResultMessage resultMessage = new ResultMessage();
        indexNavService.add(indexNavBean);
        resultMessage.setMsg("新增分类图标成功！");
        return resultMessage;
    }

    @RequestMapping(value = "/modify", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage modifyData(@RequestBody IndexNavBean indexNavBean) {
        ResultMessage resultMessage = new ResultMessage();
        indexNavService.modify(indexNavBean);
        resultMessage.setMsg("修改分类图标成功！");
        return resultMessage;
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage delete(String id) {
        ResultMessage resultMessage = new ResultMessage();
        indexNavService.delete(Integer.parseInt(id));
        resultMessage.setMsg("删除分类图标成功！");
        return resultMessage;
    }
}
